/* =============================================================================
 * experience, cohort, time decomposition (baseline)
 * ===========================================================================*/
 
 
/*******************************************************************************
U.S.
*******************************************************************************/

use "data\US.dta", clear

drop if outlier
keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

rename lnearnings lwage
rename asecwt perwt 

do "code\HLT.do"

save "temp\us_HLToutput.dta", replace

keep profile_wexp* plot_wexp profile_coh* plot_coh profile_year* plot_year

foreach v of varlist profile_wexp* profile_coh* profile_year* {
	rename `v' `v'_us
}

save "temp\us_profile_HLT.dta", replace

use "temp\us_profile_HLT.dta",clear
drop if profile_wexp_us == .
keep profile_wexp_us plot_wexp
save "temp\us_exp_profile.dta",replace

use "temp\us_profile_HLT.dta",clear
drop if profile_coh_us == .
keep profile_coh_us plot_coh
save "temp\us_coh_profile.dta",replace

use "temp\us_profile_HLT.dta",clear
keep profile_year_us plot_year
save "temp\us_year_profile.dta",replace


/*******************************************************************************
China
*******************************************************************************/


use "data\China.dta", clear

drop if outlier
keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

rename lnearnings lwage
gen perwt = 1

do "code\HLT.do"

save "temp\cn_HLToutput.dta", replace

keep profile_wexp* plot_wexp profile_coh* plot_coh profile_year* plot_year

foreach v of varlist profile_wexp* profile_coh* profile_year* {
	rename `v' `v'_cn
}

save "temp\cn_profile_HLT.dta", replace

use "temp\cn_profile_HLT.dta",clear
drop if profile_wexp_cn == .
keep profile_wexp_cn plot_wexp
save "temp\cn_exp_profile.dta",replace

use "temp\cn_profile_HLT.dta",clear
drop if profile_coh_cn == .
keep profile_coh_cn plot_coh
save "temp\cn_coh_profile.dta",replace

use "temp\cn_profile_HLT.dta",clear
keep profile_year_cn plot_year
save "temp\cn_year_profile.dta",replace



/*******************************************************************************

								Graphs
		
*******************************************************************************/

*** Experience ***
use "temp\us_exp_profile.dta", clear
merge 1:1 plot_wexp using "temp\cn_exp_profile.dta"

dis %9.2f profile_wexp_us[_N]
dis %9.2f profile_wexp_cn[_N]

replace plot_wexp = plot_wexp + 2

twoway (scatter profile_wexp_us plot_wexp, msymbol(dh) mcolor(blue) msize(large) connect(l) lcolor(blue))	///
	(scatter profile_wexp_cn plot_wexp, msymbol(oh) mcolor(red) msize(large) connect(l) lcolor(red)),		///
	xlabel(0(5)40,labsize(large)) ylabel(1(1)4,labsize(large)) 		///
	xtitle("Potential Experience",size(large)) ytitle("")	///
	legend(order(1 "U.S." 2 "China")  rows(1) pos(6) size(medlarge))		///
	title("Experience Effects",size(large) color(black)) name(exp, replace) xsize(3.6)

*** Cohort ***
use "temp\us_coh_profile.dta", clear
merge 1:1 plot_coh using "temp\cn_coh_profile.dta"

dis %9.2f profile_coh_us[_N]
dis %9.2f profile_coh_cn[_N]

replace plot_coh = plot_coh + 2

twoway (scatter profile_coh_us plot_coh, msymbol(dh) mcolor(blue) msize(large) connect(l) lcolor(blue))	///
	(scatter profile_coh_cn plot_coh, msymbol(oh) mcolor(red) msize(large) connect(l) lcolor(red)),		///
	xlabel(1935(10)1985,labsize(large))  xmtick(##2) ylabel(1(0.25)2,labsize(large)) 	///
	xtitle("Birth Year",size(large)) ytitle("")		///
	legend(order(1 "U.S." 2 "China") rows(1) pos(6) size(medlarge))		///
	title("Cohort Effects",size(large) color(black)) name(coh, replace) xsize(3.6)
		
*** Time ***
use "temp\us_year_profile.dta", clear
merge 1:1 plot_year using "temp\cn_year_profile.dta"

gen normalize_us = profile_year_us[1]
gen normalize_cn = profile_year_cn[1]

replace profile_year_us = profile_year_us/normalize_us
replace profile_year_cn = profile_year_cn/normalize_cn

dis %9.2f profile_year_us[_N]
dis %9.2f profile_year_cn[_N]

twoway (scatter profile_year_us plot_year, msymbol(dh) mcolor(blue) msize(large) connect(l) lcolor(blue))		///
	(scatter profile_year_cn plot_year, msymbol(oh) mcolor(red) msize(large) connect(l) lcolor(red)), ///
	xlabel(1985(5)2010,labsize(large)) ylabel(1(1)4,labsize(large)) 	///
	xtitle("Year",size(large)) ytitle("")	///
	legend(order(1 "U.S." 2 "China")  rows(1) pos(6) size(medlarge))		///
	title("Time Effects",size(large) color(black)) name(year, replace) xsize(3.6)

graph combine exp coh year,	xsize(20) ysize(8) row(1) 
	
graph export "figures\decomposition.pdf", as(pdf) replace	

